<?php

namespace app\controller\admin\ec;


use think\Request;
use \think\facade\Validate;
use app\eslib\EsUtils;
use app\model\ec\CategoriesModel;

class Categories
{
    // 控制器中间件
    protected $middleware = [\app\middleware\JwtAuthForAdmin::class];
    
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        // 查询 KV 数据
        $mode = $request->param('_mode', '');
        if ($mode == 'dict') {
            return json(EsUtils::stdMessage(true, '', CategoriesModel::getDictData()));
        }

        // 查询列表数据
        $searchText = $request->param('search_text', '');
        $page = intval($request->param('page', 1));
        $pageSize = intval($request->param('page_size', 10));
        $data=CategoriesModel::getListData($page,$pageSize,$searchText);
        return json(EsUtils::stdMessage(true, '', $data));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->all();
        $validator = Validate::rule([
            'pid' => 'required|numeric',
            'category_name' => 'required|max:150',
            'icon' => 'max:150',
        ], [
            'required' => ':attribute 必须填写',
            'numeric' => ':attribute 必须是数值',
            'max' => ':attribute 不能超过 :max 个字符',
        ], [
            'pid' => '上级类别',
            'category_name' => '类别名称',
            'icon' => '类别图片'
        ]);
        if (!$validator->check($formData)) {
        return json(EsUtils::stdMessage(false, $validator->getError()));
}
        // 表单数据处理
        // ...
        try {
            $model = CategoriesModel::create($formData);
            return json(EsUtils::stdMessage(true, '', $model->id));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!is_numeric($id)) {
            return json(EsUtils::stdMessage(false, 'id值无效', $id));
        }
        if ($id > 0) {
            $model = CategoriesModel::find($id);
            if (!$model) {
                return json(EsUtils::stdMessage(false, '记录不存在'));
            }
        } else {
            $model = new CategoriesModel();
            $model->sort = 999;
            $model->status = 1;
        }
        $data = [
            'data' => $model,
            'status_data' => $model->getStatusDataAttr(),
            'pid_data' => CategoriesModel::getDictData(),
        ];
        return json(EsUtils::stdMessage(true, $id, $data));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // 特殊模式的更新操作
        $mode = $request->param('_mode');
        switch ($mode) {
            case 'copy':
                // 复制指定的资源
                try {
                    $affected = CategoriesModel::copy($id);
                    return json(EsUtils::stdMessage(true, '', $affected));
                } catch (\Exception $ex) {
                    return json(EsUtils::stdMessage(false, $ex->getMessage()));
                }
            default:
                break;
        }

        // 常规更新操作
        $formData = $request->all();
        $validator = Validate::rule([
            'pid' => 'required|numeric',
            'category_name' => 'required|max:150',
            'icon' => 'max:150',
        ], [
            'required' => ':attribute 必须填写',
            'numeric' => ':attribute 必须是数值',
            'max' => ':attribute 不能超过 :max 个字符',
        ], [
            'pid' => '上级类别',
            'category_name' => '类别名称',
            'icon' => '类别图片'
        ]);
        if (!$validator->check($formData)) {
        return json(EsUtils::stdMessage(false, $validator->getError()));
}
        try {
            $affected = CategoriesModel::where('id', $id)->update($formData);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $affected = CategoriesModel::destroy($id);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }
}
